package com.tang.websocket.v1.session;

import org.springframework.beans.factory.DisposableBean;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Tang
 * @classname SessionManagerSupport
 * @description [ 管理session ]
 * @date 2022/11/29 19:18
 */
public abstract class SessionManagerSupport implements DisposableBean {

    protected Map<String, WebSocketSession> storeSession = new ConcurrentHashMap<>(8);

    /**
     * addSession
     * @param session
     * @param key
     */
    public abstract void addSession(WebSocketSession session, String key);

    /**
     * removeSession
     * @param key
     */
    public abstract void removeSession(String key);

    /**
     * getSession
     * @param key
     * @return
     */
    public abstract WebSocketSession getSession(String key);


    @Override
    public void destroy() throws Exception {
        storeSession.forEach((s, webSocketSession) -> {
            try {
                webSocketSession.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}
